package operation

import (
	"../common"
	"../web"
	"strings"
)

type OperReplaceSsnObj struct {
	common.BaseHandler
}

func (self *OperReplaceSsnObj) Prepare() {}

func (self *OperReplaceSsnObj) ParameterCheck() bool {
	s_ossn := strings.TrimSpace(self.Ctx.Params["oldssn"])
	s_nssn := strings.TrimSpace(self.Ctx.Params["newssn"])
	if s_ossn == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the Old SSN.")
		return false
	}
	if s_nssn == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the New SSN.")
		return false
	}
	if s_nssn == s_ossn {
		self.TmplData["error"] = common.GetText(self.Lang, "Can't replace ssn by self.")
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_ossn)
	self.Sqlparas = append(self.Sqlparas, s_nssn)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)
	self.Flashparas = []interface{}{strings.ToUpper(s_ossn), strings.ToUpper(s_nssn)}

	return true
}

func (self *OperReplaceSsnObj) SqlExecute() bool {
	spRtnData := *common.FetchOneRowPtr(
		`sp_ReplaceSSN ?, ?, ?`,
		self.Usr.Conn, new(common.SPReturn), self.Sqlparas...)
	if spRtnData.(common.SPReturn).Flag > 0 {
		self.TmplData["error"] = common.GetTextSP(self.Lang, spRtnData)
		return false
	}
	return true
}

func ReplaceSSNHandler(ctx *web.Context) {
	self := new(OperReplaceSsnObj)
	self.Ctx = ctx
	self.Url = "/ssnreplace"
	self.Html = "ssnreplace.html"
	self.Rightlist = []string{"ssnreplace"}
	self.Flashstr = "Reaplace SSN: %v --> %v ."
	self.Do(self)
}
